
import com.sun.jna.*;
import java.io.IOException;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author mehadihasan
 */
public class Main {
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        int CREATE_BREAKWAY_FROM_JOB = 0x01000000;
        int JOB_OBJECT_BASIC_LIMIT_INFORMATION = 2;
        int CREATE_NO_WINDOW = 0x08000000;
        int CREATE_SUSPENDED = 0x00000004;
        int JOB_OBJECT_LIMIT_PROCESS_TIME = 0x00000002;
        int JOB_OBJECT_LIMIT_ACTIVE_PROCESS = 0x00000008;
        int REALTIME_PRIORITY_CLASS=0x00000100;
        
        
        Ren.Kernel32 kernel32= Ren.Kernel32.INSTANCE;
        Ren.STARTUPINFO si = new Ren.STARTUPINFO();
        si.cb = 65;
        Ren.PROCESS_INFORMATION pi = new Ren.PROCESS_INFORMATION();

        
        try{
        if(!kernel32.CreateProcessW(new WString("D:\\Msc\\Thesis work\\testing\\pidtest\\quake2.exe"),
                null,
                null,
                null,
                false,
                CREATE_SUSPENDED|CREATE_BREAKWAY_FROM_JOB,
                null,
                new WString("D:\\Msc\\Thesis work\\testing\\pidtest\\"),
                si.getPointer(),
                pi))
            throw new IOException(Ren.getLastError());

        Ren.SECURITY_ATTRIBUTES sa=new Ren.SECURITY_ATTRIBUTES();
        Pointer hjob = kernel32.CreateJobObjectW(sa, new WString("Hello"));
        
        System.out.println(pi.dwProcessId);
        System.out.println(pi.dwThreadId);
        System.out.println(pi.hThread);
        System.out.println(pi.hProcess);
        
        if(!kernel32.AssignProcessToJobObject(hjob, pi.hProcess))
            System.out.println("couldnt assign process to job");
        
        Ren.JOBOBJECT_BASIC_LIMIT_INFORMATION hp=new Ren.JOBOBJECT_BASIC_LIMIT_INFORMATION();
             
        kernel32.QueryInformationJobObject(hjob, JOB_OBJECT_BASIC_LIMIT_INFORMATION, hp, 48, null);
        hp.LimitFlags = JOB_OBJECT_LIMIT_PROCESS_TIME|JOB_OBJECT_LIMIT_ACTIVE_PROCESS;
        hp.PerProcessUserTimeLimit=10000000;
        hp.ActiveProcessLimit = 1;
        if(!kernel32.SetInformationJobObject(hjob, JOB_OBJECT_BASIC_LIMIT_INFORMATION, hp, 48))
        {
            System.out.println("couldnt set job info");
            System.out.println(Ren.getLastError());
        }
        System.out.println(hp.ActiveProcessLimit);
        System.out.println(hp.PerJobUserTimeLimit);
        System.out.println(hp.PerProcessUserTimeLimit);
        System.out.println(hp.SchedulingClass);
        kernel32.SetPriorityClass(pi.hProcess, REALTIME_PRIORITY_CLASS);
        kernel32.ResumeThread(pi.hThread);

        System.out.println(kernel32.GetPriorityClass(pi.hProcess));
       
        kernel32.CloseHandle(pi.hProcess);
        kernel32.CloseHandle(hjob);
        System.out.println(Ren.getLastError());
//        if(kernel32.AssignProcessToJobObject(p,pi.hProcess))
//            throw new IOException(Ren.getLastError());
        }
        catch(IOException ioe){
                ioe.printStackTrace();
        }
    }

//    if(!Kernel32.INSTANCE.MoveFileW(new WString(from), new WString(to))) {
//      throw new IOException(getLastError());
//    }
    
}
